package com.devops.utils;

import com.alibaba.fastjson.JSONObject;
import com.devops.enums.PlanetDevopsEnum;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @Author: mis_wu
 * @Company: asia info
 * @Create_time: 2023-10-12
 * @Description: 自定义返回值类
 */
@Data
@Accessors(chain = true)
public class ResponseResult<T> implements Serializable {

    private Boolean success = true;
    private Integer code;
    private String msg;
    private T data;

    private static JSONObject jsonObject = new JSONObject();

    public ResponseResult(){}


    public static <T> ResponseResult<T> isSuccess(){
        ResponseResult<T> result = new ResponseResult<>();
        result.setSuccess(true)
                .setCode(PlanetDevopsEnum.SUCCESS.getCode())
                .setMsg(PlanetDevopsEnum.SUCCESS.getMsg())
                .setData(null);

        return result;
    }

    public static <T> ResponseResult<T> isSuccess(T data){
        ResponseResult<T> result = new ResponseResult<>();
        result.setSuccess(true)
                .setCode(PlanetDevopsEnum.SUCCESS.getCode())
                .setMsg(PlanetDevopsEnum.SUCCESS.getMsg())
                .setData(data);

        return result;
    }

    public static <T> ResponseResult<T> error(){
        ResponseResult<T> result = new ResponseResult<>();
        result.setSuccess(false)
                .setCode(PlanetDevopsEnum.ERROR.getCode())
                .setMsg(PlanetDevopsEnum.ERROR.getMsg())
                .setData(null);

        return result;
    }

    public static <T> ResponseResult<T> error(Integer code,String msg){
        ResponseResult<T> result = new ResponseResult<>();
        result.setSuccess(false).setCode(code).setMsg(msg).setData(null);
        return result;
    }
}
